package com.app.conf.source;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * @ClassName:DataSourceTwoMapperConfig
 * @Description TODO
 * @Author zk_yjl
 * @Date 2021/9/2 12:29
 * @Version 1.0
 * @Since 1.0
 **/
@Configuration
@MapperScan(
        basePackages="com.app.mapper.jiuquan", //配置扫描的mapper接口
        sqlSessionFactoryRef = "sqlSessionFactoryJiuquan", //使用的Factory
        sqlSessionTemplateRef = "sqlSessionTemplateJiuquan") //使用的SqlSessionTemplate
public class DataSourceJiuquanConfig {
    @Resource(name="dataSourceJiuquan")
    private DataSource dataSourceJiuquan;
    /**本数据源扫描的mapper路径*/
    static final String MAPPER_LOCATION = "classpath:mapper/jiuquan/**/*.xml";


    @Bean //分页插件 如果配置多个数据库 方法名(mybatisPlusInterceptor)不可重复,否则会出现注入异常
    public MybatisPlusInterceptor mybatisPlusInterceptorJiuquan() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 分页
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        // 方言
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        paginationInnerInterceptor.setMaxLimit(100L);
        interceptor.addInnerInterceptor(paginationInnerInterceptor);
        // 乐观锁 更新时需实体携带 version 版本号参数
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }

    @Bean(name="sqlSessionFactoryJiuquan")
    public SqlSessionFactory sqlSessionFactoryJiuquan(){
        try{
            MybatisSqlSessionFactoryBean bean=new MybatisSqlSessionFactoryBean();
            MybatisConfiguration configuration = new MybatisConfiguration();
            configuration.setMapUnderscoreToCamelCase(true);
            //设置mapper配置文件
            bean.setDataSource(dataSourceJiuquan);
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCATION));
            bean.setConfiguration(configuration);
            bean.setPlugins(mybatisPlusInterceptorJiuquan());// 配置分页插件 否则分页工具失效
            return bean.getObject();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


    /**
     * 配置事务
     * @param dataSourceJiuquan
     * @return
     */
    @Bean
    public DataSourceTransactionManager dataSourceTransactionManagerJiuquan(
            @Qualifier("dataSourceJiuquan") DataSource dataSourceJiuquan) {
        return new DataSourceTransactionManager(dataSourceJiuquan);
    }
    /**
     * 通过SqlSessionFactory 创建 SqlSessionTemplate
     * @return
     */
    @Bean(name="sqlSessionTemplateJiuquan")
    public SqlSessionTemplate sqlSessionTemplate(){
        return new SqlSessionTemplate(sqlSessionFactoryJiuquan());
    }




}
